home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Turbo C 3.0 - arithmetic in defines
- Date: 13 Feb 1996 20:13:32 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb13131332@qcd.lanl.gov>
- References: <4fqgkf$iqd@ccshst05.cs.uoguelph.ca>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: thay@uoguelph.ca's message of 13 Feb 1996 17:09:35 GMT
-
- In article <4fqgkf$iqd@ccshst05.cs.uoguelph.ca> thay@uoguelph.ca (Toby
- K Hay) writes:
- <snip>
- As a newcomer to C programming I'm having difficulty using arithmetic
- and #defined values to dimension arrays at compile time. The following
- very short program reproduces my error:
-
- Your error is very common misunderstanding amongst newcomers. I will
- try to explain what I think is your problem.
-
- #define var1 16
- #define var2 485
- #define var3 ((var2-1)/var1)+1
- int main(void)
- {
- printf("\n%i %i %i %i",var1,var2,var3,var1*var3);
- return 0;
- }
-
- Output from this is '16 485 31 481', not '16 485 31 496' as I should have
- expected. That is, var3 is calculated correctly in its #define, but the
-
- This is precisely your problem. A #define _never_ calculates
- anythings: it simply defines a way of dumping in a sequence of tokens
- when you need it. What the first define says is that when you later
- use the token var1 in your code, and it is time for expanding the
- token, it should be replaced by the token 16 instead. Thus, if you
- wrote x+var1, the compiler would see token x, token +, token var1, and
- then say "Aha! var1 is a `macro'!", and so replace var1 with 16. Then
- it will say "Good! There is nothing more relevant to expand, so I have
- found the three tokens x, + and 16, which means that the programmer
- wants me to generate code for adding 16 to whatever x is." var2 is
- similar.
-
- Now see what happens when it sees var3? It says I have the sequence of
- 11 tokens on replacement: viz. ( ( var2 - 1 ) / ( var1 ) + 1, where I
- have used spaces to show the separation between the tokens. Now, it
- goes back and looks at the sequence and recognizes var2 and var1 as
- macros, and replaces them further: ending up with ( ( 485 - 1 ) / ( 16
- ) + 1. It is only at this stage does it start interpreting this
- expression: and either calculates it or compiles instruction for
- calculating it. The result is of course 31.
-
- So what happens when you say var1*var3? Well, if you follow through
- the same argument, you find that you end up with 16*((485-1)/16)+1
- (this time I amassuming you know what the tokens are, so I am not
- showing the extraneous spaces that I was showing earlier) which it
- then interprets, getting a 481, naturally.
-
- var1*var3 in the printf() statement is not calculated correctly. Where
- am I going wrong?
-
- Is it clear now? Remember to always define macros with an extra set of
- parentheses whenever possible, unless they are one token long. Thus
- #define var1 16 is fine (one token long),
- #define complicated do { x:= 1; } while(0) is also fine (parentheses
- not allowed), but #define var3 (((var2-1)/var1)+1) is what you ought
- to do.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-